<template>
  <div class="menu-container" :style="{ display: show ? 'block' : 'none' }">
    <div
      class="menu-mask"
      :style="{ opacity: show ? 1 : 0 }"
      @click="clickHide"
    ></div>
    <transition name="van-slide-left">
      <div class="menu-content" v-show="show">
        <div class="menu-title">{{ title }}</div>
        <div class="menu-text">
          共{{ menu.length }}章 <span class="mulu">目录</span>
        </div>

        <div
          class="menu-item"
          v-for="(item, index) in menu"
          :key="index"
          @click.stop
          @click="changeChapterIndex(index)"
        >
          <p :class="currentIndex == index ? 'active' : ''">
            {{ item.title }}
          </p>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "标题",
    },
    currentIndex: {
      type: Number,
      default: 0,
    },
    menu: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {};
  },

  methods: {
    clickHide() {
      this.$emit("clickHide", false);
    },
    changeChapterIndex(index) {
      this.$emit("changeChapterIndex", index);
    },
  },
};
</script>

<style lang="scss" scoped>
.menu-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
.menu-mask {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: all 0.5s;
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.5);
}
.menu-content {
  position: absolute;
  z-index: 2;
  width: 60%;
  padding: 10px;
  box-sizing: border-box;
  height: 100vh;
  overflow: scroll;
  z-index: 99;
  background-color: #fff;
  div {
    text-align: left;
    font-size: 14px;
    // line-height: 1.5;
    // background-color: red;
  }
  p {
    text-indent: 0;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .active {
    color: #e64d2e;
  }
  .menu-title {
    text-align: center;
    font-size: 18px;
    margin-bottom: 4px;
  }
  .menu-text {
    font-size: 14px;
    margin-top: 14px;
    display: flex;
    justify-content: space-between;
  }
  .menu-item {
    padding: 4px 0;
    border-bottom: 1px dashed #999;
  }
}
</style>
